/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dafe.partyband.services.impl;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.openide.util.Exceptions;

public class SimpleURIBuilder {
    
    private static final String QUERY_START = "?";
    private static final String PAIR_DELIMITER = "&";
    private static final String ASIGNMENT = "=";
    
    private final String urlBase;
    private final List<String> params;
    private final List<String> values;

    public SimpleURIBuilder(String urlBase) {
        this.urlBase = urlBase;
        params = new ArrayList<String>();
        values = new ArrayList<String>();
    }
    
    public SimpleURIBuilder addQuery(String param, String value) {
        params.add(param);
        values.add(URLEncoder.encode(value));
        
        return this;
    }
    
    public URI buildURI() {
        StringBuilder sb = new StringBuilder(urlBase);
        sb.append(QUERY_START);
        for (int i = 0; i < params.size(); i++) {
            sb.append(params.get(i)).append(ASIGNMENT).append(values.get(i));
            if (i + 1 < params.size()) {
                sb.append(PAIR_DELIMITER);
            }
        }
        
        try {
            return new URI(sb.toString());
        } catch (URISyntaxException ex) {
            Exceptions.printStackTrace(ex);
        }
        
        return null;
    }
    
}
